﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 人事工资管理系统
{
    public partial class frmDepManager : Form
    {
        public frmDepManager()
        {
            InitializeComponent();
        }
        PMSClass.DBOperate operate = new PMSClass.DBOperate();
        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" )
            {
                MessageBox.Show("请将信息填写完整", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                string sql = "select count(*)from tb_department where DName='" + textBox1.Text + "'";
                int i = operate.HumanNum(sql);
                if (i > 0)
                {
                    MessageBox.Show("部门已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    string str = "insert into tb_department(DName)values('" + textBox1.Text.ToString() + "')";
                    int num = operate.OperateData(str);
                    if (num > 0)
                    {
                        MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
        }

        private void toolStripLabel4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count == 0)
            {
                MessageBox.Show("请选择要删除的信息", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                string name = dataGridView1.SelectedCells[0].Value.ToString();
                string sql = "delete from tb_department( where Dname='" + name + "'";
                operate.OperateData(sql);
                MessageBox.Show("删除成功", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void frmDepManager_Load(object sender, EventArgs e)
        {
            string str =
          "select ID as '编码',DName as '部门名'from tb_department";
            operate.BindDataGridView(dataGridView1, str);
            dataGridView1.Columns[0].Width = 40;
            dataGridView1.Columns[1].Width = 80;
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count == 0)
            {
                MessageBox.Show("请选择要修改的信息", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                string name = dataGridView1.SelectedCells[1].Value.ToString();
                string sql = "update  tb_department set Dname='"+textBox2.Text.Trim()+"'where Dname='" + name + "'";
                operate.OperateData(sql);
                MessageBox.Show("修改成功", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
